package com.zheng.api.server.service.base.pay;

import com.zheng.common.constant.PayConstant;
import com.zheng.pay.dao.model.PayRefundOrder;
import com.zheng.pay.dao.model.PayRefundOrderExample;
import com.zheng.pay.rpc.api.PayRefundOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author: lvb
 * @date: 17/10/30
 * @description:
 */
@Service
public class Base4RefundOrderService extends BaseCommonService {

    @Autowired
    private PayRefundOrderService payRefundOrderService;

    public int baseCreateRefundOrder(PayRefundOrder refundOrder) {
        return payRefundOrderService.insertSelective(refundOrder);
    }

    public PayRefundOrder baseSelectRefundOrder(String refundOrderId) {
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRefundorderidEqualTo(refundOrderId);
        return payRefundOrderService.selectFirstByExample(example);
    }

    public PayRefundOrder baseSelectByMchIdAndRefundOrderId(String mchId, String refundOrderId) {
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        criteria.andRefundorderidEqualTo(refundOrderId);
        List<PayRefundOrder> refundOrderList = payRefundOrderService.selectByExample(example);
        return CollectionUtils.isEmpty(refundOrderList) ? null : refundOrderList.get(0);
    }

    public PayRefundOrder baseSelectByMchIdAndMchRefundNo(String mchId, String mchRefundNo) {
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        criteria.andMchrefundnoEqualTo(mchRefundNo);
        List<PayRefundOrder> refundOrderList = payRefundOrderService.selectByExample(example);
        return CollectionUtils.isEmpty(refundOrderList) ? null : refundOrderList.get(0);
    }

    public int baseUpdateStatus4Ing(String refundOrderId, String channelOrderNo) {
        PayRefundOrder refundOrder = new PayRefundOrder();
        refundOrder.setStatus(PayConstant.REFUND_STATUS_REFUNDING);
        if(channelOrderNo != null) refundOrder.setChannelorderno(channelOrderNo);
        refundOrder.setRefundsucctime(new Date());
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRefundorderidEqualTo(refundOrderId);
        criteria.andStatusEqualTo(PayConstant.REFUND_STATUS_INIT);
        return payRefundOrderService.updateByExampleSelective(refundOrder, example);
    }

    public int baseUpdateStatus4Success(String refundOrderId) {
        return baseUpdateStatus4Success(refundOrderId, null);
    }

    public int baseUpdateStatus4Success(String refundOrderId, String channelOrderNo) {
        PayRefundOrder refundOrder = new PayRefundOrder();
        refundOrder.setRefundorderid(refundOrderId);
        refundOrder.setStatus(PayConstant.REFUND_STATUS_SUCCESS);
        refundOrder.setResult(PayConstant.REFUND_RESULT_SUCCESS);
        refundOrder.setRefundsucctime(new Date());
        if(StringUtils.isNotBlank(channelOrderNo)) refundOrder.setChannelorderno(channelOrderNo);
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRefundorderidEqualTo(refundOrderId);
        criteria.andStatusEqualTo(PayConstant.REFUND_STATUS_REFUNDING);
        return payRefundOrderService.updateByExampleSelective(refundOrder, example);
    }

    public int baseUpdateStatus4Complete(String refundOrderId) {
        PayRefundOrder refundOrder = new PayRefundOrder();
        refundOrder.setRefundorderid(refundOrderId);
        refundOrder.setStatus(PayConstant.REFUND_STATUS_COMPLETE);
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRefundorderidEqualTo(refundOrderId);
        List values = CollectionUtils.arrayToList(new Byte[] {
                PayConstant.REFUND_STATUS_SUCCESS, PayConstant.REFUND_STATUS_FAIL
        });
        criteria.andStatusIn(values);
        return payRefundOrderService.updateByExampleSelective(refundOrder, example);
    }

    public int baseUpdateStatus4Fail(String refundOrderId, String channelErrCode, String channelErrMsg) {
        PayRefundOrder refundOrder = new PayRefundOrder();
        refundOrder.setStatus(PayConstant.REFUND_STATUS_FAIL);
        refundOrder.setResult(PayConstant.REFUND_RESULT_FAIL);
        if(channelErrCode != null) refundOrder.setChannelerrcode(channelErrCode);
        if(channelErrMsg != null) refundOrder.setChannelerrmsg(channelErrMsg);
        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRefundorderidEqualTo(refundOrderId);
        criteria.andStatusEqualTo(PayConstant.REFUND_STATUS_REFUNDING);
        return payRefundOrderService.updateByExampleSelective(refundOrder, example);
    }

}
